package com.cpi.cornpp.purchaseorder.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.biweek.exception.BiWeekException;
import com.cpi.cornpp.biweeks.services.BiWeekPeriodVersionService;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.corntypes.services.CornTypesService;
import com.cpi.cornpp.plants.services.PlantService;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.form.PurchaseOrderVersionForm;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.TransportView;

public class ShowAddPurchaseOrderVersionAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String FAILURE_FORWARD = "error";
	
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException{
		String versionId = null;
		String transportId = null;
		PurchaseOrderVersionForm povForm = null;
		CornTypesService cornTypesService = null;
		TransportView transportView = null;
		TransportService transportService = null;
		BiWeekPeriodVersionService biWeekVersionService = null;
		PlantService plantService = null;
		List cornTypes = null;
		List plantsView = null;
		List transportsView = null;
		List transportCostElementsView = null;
		List versionPurchaseDatesView = null;
		
		HttpSession session = null;
		ActionForward forward = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		session = request.getSession(false);
		
		try{
			
			if (form != null){
				cornTypesService = new CornTypesService();
				plantService = new PlantService();
				transportService = new TransportService();
				biWeekVersionService = new BiWeekPeriodVersionService();
				povForm = (PurchaseOrderVersionForm) form;
				versionId = povForm.getVersionId();
				transportId = povForm.getPurchaseOrderVersionTransportId();
				
				if ( !(StringUtils.isNotEmpty(versionId) && (StringUtils.isNumeric(versionId))) ){
					versionId = (String)session.getAttribute("versionId");
				}
				transportsView = transportService.getTransports(1, true);
				
				if (StringUtils.isNotEmpty(transportId)){
					transportView = transportService.getTransport(Integer.parseInt(transportId));
					transportCostElementsView = transportService.getTransportCostElements(Integer.parseInt(transportId));
					
					transportView.setTransportCostElements(transportCostElementsView);
				} else if (CollectionUtils.isNotEmpty(transportsView)){
					transportView = (TransportView) transportsView.get(0);
				}
				
				plantsView = plantService.list(0, true);
				versionPurchaseDatesView = biWeekVersionService.getBiWeekPeriodVersionsByVersion(new Integer(versionId));
				cornTypes = cornTypesService.getListCornTypes();
				
				request.setAttribute("cornTypes", cornTypes);
				request.setAttribute("transportView", transportView);
				request.setAttribute("transports", transportsView);
				request.setAttribute("versionPurchaseDates", versionPurchaseDatesView);
				request.setAttribute("plants", plantsView);
				
				forward = mapping.findForward(SUCCESS_FORWARD);
			}
			
		} catch(TransportException tre){
			PurchaseProgramException ppe = new PurchaseProgramException(tre, PurchaseProgramException.LAYER_ACTION, PurchaseProgramException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, tre);
			throw ppe;
		} catch(BiWeekException bwe){
			PurchaseProgramException ppe = new PurchaseProgramException(bwe, PurchaseProgramException.LAYER_ACTION, PurchaseProgramException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, bwe);
			throw ppe;
		} catch(Exception e){
			PurchaseProgramException ppe=null;
			e.printStackTrace(System.out);
			ppe= new PurchaseProgramException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, ppe);
			throw ppe;
		}
		
		return forward;
	}
}